package org.vectrics.recruiter.document;

import java.io.FileInputStream;
import java.util.Date;

import org.apache.log4j.Logger;
import org.vectrics.SystemException;
import org.vectrics.common.db.OwnedObject;
import org.vectrics.common.db.RecordObject;
import org.vectrics.common.util.FileUtil;
import org.vectrics.domain.Domain;
import org.vectrics.recruiter.RecruitServiceLocator;
import org.vectrics.recruiter.config.RecruitConfig;
import org.vectrics.user.Person;
import org.vectrics.user.PersonEncounter;
/**
 * 
 * @author mmore
 * CandidateDocument es un objeto que sirve para guardar documentos de la persona, su nombre fue heredado de vectrics.
 * No se usa solamente para el candidato.
 */

public class CandidateDocument implements OwnedObject, RecordObject {
	public static String UNKNOWN_TYPE = "unknown";
	public static String RESUME_TYPE = "resume";
	public static String COVER_LETTER_TYPE = "cover.letter";
	public static String LETTER_RECOMMENDATION_TYPE = "letter.recom";
	public static String REFERENCES_TYPE = "references";
	
	public static int COLUMN_DESCRIPTION_LENGTH = 140;
	private transient Logger log = Logger.getLogger(this.getClass());
	
	private Long id = null;
	private Person person = null;
	private String originalFileName = null;
	private String savedFileName = null;
	private CandidateDocumentCategory category = null;
	private String name = null;
	private String description = null;
	private Date dateCreated = null;
	private Date lastUpdate = new Date();
	private Date lastIndex = null;
	private boolean indexError = false;
	private Domain domain = null;
	private boolean voided = false;

	public CandidateDocument() {
		super();
	}

	public Date getStartDate() {
		return(this.getDateCreated());
	}
	
	public String getTypeDescription() {
		return(this.getDescription());
	}
	
	public Date getEndDate() {
		return(this.getStartDate());
	}
	
	public String getTextContent() {
		StringBuffer content = new StringBuffer();
		try {
			String documentDir = RecruitConfig.getRecruitInstance().getCandidateDocumentPath(this.getDomain());
			FileInputStream file = new FileInputStream(documentDir + "/" + this.getSavedFileName());
			boolean done = false;
			while (!done) {
				int c = file.read();
				if (c < 0) {
					done = true;
				} else {
					content.append((char)c);
				}
			}
			file.close();
		} catch (Exception ex) {
			log.error("Exception getting attachment content", ex);
		}
		return(content.toString());
	}
	
	
	/**
	 * @return Returns the id.
	 */
	public Long getId() {
		return id;
	}

	/**
	 * @param id The id to set.
	 */
	public void setId(Long id) {
		this.id = id;
	}

	/**
	 * @return Returns the domain.
	 */
	public Domain getDomain() {
		return domain;
	}

	public void markForReIndexing() {
		this.lastIndex = null;
	}
	
	/**
	 * @param domain The domain to set.
	 */
	public void setDomain(Domain domain) {
		this.domain = domain;
	}

	/**
	 * @return Returns the dateCreated.
	 */
	public Date getDateCreated() {
		return dateCreated;
	}

	/**
	 * @param dateCreated The dateCreated to set.
	 */
	public void setDateCreated(Date dateCreated) {
		this.dateCreated = dateCreated;
	}

	/**
	 * @return Returns the description.
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * @param description The description to set.
	 */
	public void setDescription(String description) {
		this.description = description;
	}

	/**
	 * @return Returns the indexError.
	 */
	public boolean getIndexError() {
		return indexError;
	}

	/**
	 * @param indexError The indexError to set.
	 */
	public void setIndexError(boolean indexError) {
		this.indexError = indexError;
	}

	/**
	 * @return Returns the lastIndex.
	 */
	public Date getLastIndex() {
		return lastIndex;
	}

	/**
	 * @param lastIndex The lastIndex to set.
	 */
	public void setLastIndex(Date lastIndex) {
		this.lastIndex = lastIndex;
	}

	/**
	 * @return Returns the lastUpdate.
	 */
	public Date getLastUpdate() {
		return lastUpdate;
	}

	/**
	 * @param lastUpdate The lastUpdate to set.
	 */
	public void setLastUpdate(Date lastUpdate) {
		this.lastUpdate = lastUpdate;
	}

	/**
	 * @return Returns the name.
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name The name to set.
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return Returns the originalFileName.
	 */
	public String getOriginalFileName() {
		return originalFileName;
	}

	/**
	 * @param originalFileName The originalFileName to set.
	 */
	public void setOriginalFileName(String originalFileName) {
		this.originalFileName = originalFileName;
	}

	/**
	 * @return Returns the person.
	 */
	public Person getPerson() {
		return person;
	}

	/**
	 * @param person The person to set.
	 */
	public void setPerson(Person person) {
		this.person = person;
	}

	public String getFileExtension() {
		return(FileUtil.findFileExtention(this.savedFileName));
	}
	
	/**
	 * @return Returns the savedFileName.
	 */
	public String getSavedFileName() {
		return savedFileName;
	}

	/**
	 * @param savedFileName The savedFileName to set.
	 */
	public void setSavedFileName(String savedFileName) {
		this.savedFileName = savedFileName;
	}

	/**
	 * @return Returns the category.
	 */
	public CandidateDocumentCategory getCategory() {
		return category;
	}


	/**
	 * @param category The category to set.
	 */
	public void setCategory(CandidateDocumentCategory category) {
		this.category = category;
	}

	public void setCategory(String code) {
		this.category = RecruitServiceLocator.getRecruitUserService().findCandidateDocumentCategory(code);
		if (this.category == null)
			throw new SystemException("Could not find CandidateDocumentCategory for code: " + code);
		
	}

	/**
	 * @return Returns the voided.
	 */
	public boolean getVoided() {
		return voided;
	}

	/**
	 * @param voided The voided to set.
	 */
	public void setVoided(boolean voided) {
		this.voided = voided;
	}

	public String toString() {
		return("" + this.id + "|savedFileName=" + this.savedFileName 
				+ "|name=" + this.name 
				+ "|category=" + this.getCategory() 
				+ "|description=" + this.description
				+ "|originalFileName" + this.originalFileName);
	}
}
